<template>
  <div>
    <!-- 顶部面包屑标识与导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/sys-admin/pet-management'}">宠物详情</el-breadcrumb-item>
      <el-breadcrumb-item>添加宠物</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <!-- 添加数据的表单 -->
    <el-form :model="petFrom" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="名称" prop="petName">
        <el-input v-model="petFrom.petName"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="petType">
        <el-input v-model="petFrom.petType"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio v-model="petFrom.sex" label="1">雄性</el-radio>
        <el-radio v-model="petFrom.sex" label="2">雌性</el-radio>
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <div class="block">
          <el-date-picker
              v-model="petFrom.birthday"
              type="date"
              placeholder="选择日期">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="领养状态" prop="state">
        <el-radio v-model="petFrom.state" label="1">待领养</el-radio>
        <el-radio v-model="petFrom.state" label="2">已领养</el-radio>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="petFrom.remark"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="warning" @click="gotoList">返回列表</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      sex: '1',
      state: '1',
      // 表单
      petFrom: {},
      value: '',
      // 表单验证规则
      rules: {
        petName: [
          {required: true, message: '请输入宠物名称', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        petType:[
          {required: true, message: '请输入宠物类型', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        sex:[
          {required: true, message: '请输入宠物性别', trigger: 'change'},
        ],
        birthday:[
          {required: true, message: '请选择日期', trigger: 'change'},
        ],
        state:[
          {required: true, message: '请输入宠物领养状态', trigger: 'change'},
        ],
        remark:[
          {required: true, message: '请输入备注', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
      }
    };
  },
  methods: {
    // 返回列表
    gotoList() {
      this.$router.push('./');
    },
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:13001/pet/add-new';
          console.log('url = ' + url);

          if (this.petFrom.sex==1){
            this.petFrom.sex="雄性";
          }else {
            this.petFrom.sex="雌性";
          }
          if (this.petFrom.state==1){
            this.petFrom.state = 0;
          }else {
            this.petFrom.state=1;
          }

          let formData = this.qs.stringify(this.petFrom);

          let localJwt = JSON.parse(localStorage.getItem('localJwt'));

          this.axios
              .create({
                'headers': {
                  'Authorization': localJwt.token
                }
              })
              .post(url, formData).then((response) => {
            let jsonResult = response.data;
            console.log(jsonResult);
            if (jsonResult.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加'+jsonResult.petName+'成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: jsonResult.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>